﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace XDUD_QuanLyBanHang
{
    public partial class frmChitiethoadon : Form
    {
        string strConnectionString =
            "Data Source=VUHOANG-PC\\SQLEXPRESS;Initial Catalog=QuanLyBanHang;Integrated Security=True";
        SqlConnection conn = null;
        SqlDataAdapter daHopDong = null;
        DataTable dtHopdong = null;
        SqlDataAdapter daSanpham = null;
        DataTable dtSanpham = null;
        SqlDataAdapter datable = null;
        DataTable dttable = null;
        DataView dtv = null;

        void LoadData() 
        {
            try
            {
                //dtv.RowFilter = "";
                conn = new SqlConnection(strConnectionString);
                //string sanpham = cboMaSP.SelectedValue.ToString();

                daSanpham = new SqlDataAdapter("SELECT * FROM SanPham", conn);
                dtSanpham = new DataTable();
                dtSanpham.Clear();
                daSanpham.Fill(dtSanpham);
               
                (DgvHopDong.Columns["TenSp"] as DataGridViewComboBoxColumn).DataSource = dtSanpham;
                (DgvHopDong.Columns["TenSp"] as DataGridViewComboBoxColumn).DisplayMember = "TenSP";
                (DgvHopDong.Columns["TenSp"] as DataGridViewComboBoxColumn).ValueMember = "TenSP";

                daHopDong = new SqlDataAdapter("SELECT * FROM ChiTietHoaDon", conn);
                dtHopdong = new DataTable();
                dtHopdong.Clear();
                daHopDong.Fill(dtHopdong);
                (DgvHopDong.Columns["MaHD"] as DataGridViewComboBoxColumn).DataSource = dtHopdong;
                (DgvHopDong.Columns["MaHD"] as DataGridViewComboBoxColumn).DisplayMember = "MaHD";
                (DgvHopDong.Columns["MaHD"] as DataGridViewComboBoxColumn).ValueMember = "MaHD";

                cboMaHD.DataSource = dtHopdong;
                cboMaHD.DisplayMember = "MaHD";
                cboMaHD.ValueMember = "MaHD";

                cboMaSP.DataSource = dtHopdong;
                cboMaSP.DisplayMember = "MaSP";
                cboMaSP.ValueMember = "MaSP";

                datable = new SqlDataAdapter("select ChiTietHoaDon.Soluong, MaHD, TenSP FROM ChiTietHoaDon, SanPham where ChiTietHoaDon.MaSP = SanPham.MaSP",conn);
                dttable = new DataTable();
                datable.Fill(dttable);
                DgvHopDong.DataSource = dttable;


                //datable = new SqlDataAdapter("SELECT ChiTietHoaDon.Soluong,MaHD,TenSP FROM ChiTietHoaDon, SanPham where SanPham.MaSp = ChiTietHoaDon.MaSp", conn);
                //dttable = new DataTable();
                //dttable.Clear();
                //datable.Fill(dttable);
                ////dtv = new DataView(dttable);
                //DgvHopDong.DataSource = dttable;
                
            }
            catch (SqlException)
            { 
                MessageBox.Show("Không lấy được nội dung trong table KHACHHANG. Lỗi rồi!!!");         
            }

        }
        public frmChitiethoadon()
        {
            InitializeComponent();
        }

        private void frmChitiethoadon_Load(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
